home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / VMID.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  139 lines

  1. /*
  2.  * @(#)VMID.java    1.7 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.rmi.dgc;
  16.  
  17. import java.io.*;
  18. import java.net.*;
  19. import java.rmi.server.UID;
  20.  
  21. /**
  22.  * A VMID is a identifier that is unique across all Java virtual
  23.  * machines.  VMIDs are used by the distributed garbage collector
  24.  * to identify client VMs.
  25.  *
  26.  * @version    1.7, 07/01/98
  27.  * @author    Ann Wollrath
  28.  * @author    Peter Jones
  29.  */
  30. public final class VMID implements java.io.Serializable {
  31.  
  32.     /** array of bytes uniquely identifying this host */
  33.     private static byte[] localAddr;
  34.     /** true if address for this host actually is unique */
  35.     private static boolean localAddrUnique;
  36.     static {
  37.     try {
  38.         InetAddress localInetAddress = InetAddress.getLocalHost();
  39.         byte[] raw = localInetAddress.getAddress();
  40.         localAddr = raw;
  41.  
  42.         if (raw == null ||        // if local host unknown,
  43.         ((raw[0] | raw[1] | raw[2] | raw[3]) == 0) ||
  44.         ((raw[0] == 127) &&    // or if it is localhost (127.0.0.1)
  45.          (raw[1] ==   0) &&    // (maybe because of applet)
  46.          (raw[2] ==   0) &&    // security manager?)
  47.          (raw[3] ==   1)))
  48.         localAddrUnique = false; // then can't get unique host address
  49.         else
  50.         localAddrUnique = true;
  51.     } catch (Exception e) {
  52.         localAddr = null;
  53.         localAddrUnique = false;
  54.     }
  55.     }
  56.  
  57.     /** array of bytes uniquely identifying host created on */
  58.     private byte[] addr;
  59.  
  60.     /** unique identifier with respect to host created on */
  61.     private UID uid;
  62.  
  63.     /** use serialVersionUID from prebeta for interoperability */
  64.     private static final long serialVersionUID = -538642295484486218L;
  65.  
  66.     /**
  67.      * Create a new VMID.  Each new VMID returned from this constructor
  68.      * is unique for all Java virtual machines under the following
  69.      * conditions: a) the conditions for uniqueness for objects of
  70.      * the class <b>java.rmi.server.UID</b> are satisfied, and b) an
  71.      * address can be obtained for this host that is unique and constant
  72.      * for the lifetime of this object.  <p>
  73.      * The static method <b>isUnique</b> can be invoked to determine
  74.      * if an accurate address can be obtained for this host.
  75.      */
  76.     public VMID()
  77.     {
  78.     addr = localAddr;
  79.     uid = new UID();
  80.     }
  81.  
  82.     /**
  83.      * Return true if an accurate address can be determined for this
  84.      * host.  If false, reliable VMID cannot be generated from this host
  85.      * @return true if host address can be determined, false otherwise
  86.      */
  87.     public static boolean isUnique()
  88.     {
  89.     return localAddrUnique;
  90.     }
  91.  
  92.     /**
  93.      * Compute hash code for this VMID.
  94.      */
  95.     public int hashCode() {
  96.     return uid.hashCode();
  97.     }
  98.  
  99.     /**
  100.      * Compare this VMID to another, and return true if they are the
  101.      * same identifier.
  102.      */
  103.     public boolean equals(Object obj) {
  104.     if ((obj != null) && (obj instanceof VMID)) {
  105.         VMID vmid = (VMID) obj;
  106.         if (!uid.equals(vmid.uid))
  107.         return false;
  108.         if ((addr == null) ^ (vmid.addr == null))
  109.         return false;
  110.         if (addr != null) {
  111.         if (addr.length != vmid.addr.length)
  112.             return false;
  113.         for (int i = 0; i < addr.length; ++ i)
  114.             if (addr[i] != vmid.addr[i])
  115.             return false;
  116.         }
  117.         return true;
  118.     } else {
  119.         return false;
  120.     }
  121.     }
  122.  
  123.     /**
  124.      * Return string representation of this VMID.
  125.      */
  126.     public String toString() {
  127.     StringBuffer result = new StringBuffer();
  128.     if (addr != null)
  129.         for (int i = 0; i < addr.length; ++ i) {
  130.         if (i > 0)
  131.             result.append('.');
  132.         result.append(Integer.toString(((int) addr[i]) & 0xFF, 10));
  133.         }
  134.     result.append(':');
  135.     result.append(uid.toString());
  136.     return result.toString();
  137.     }
  138. }
  139.